<template>
  <div class="app-container">
    <div class="funOperate">
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item label="驾校名称:" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入驾校名称"
            clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="时间:">
          <el-date-picker
            v-model="dateRange"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table
      v-loading="loading"
      :data="campusList"
      stripe
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="驾校名称" align="center" prop="name" />
      <el-table-column label="驾校地址" align="center" prop="address" />
      <el-table-column label="校区联系电话" align="center" prop="phone" />
      <el-table-column label="校区管理人" align="center" prop="manager" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <el-table-column label="修改日期" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList" />
    <!-- 添加或修改驾校信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="100px">
        <el-form-item label="驾校名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入驾校名称" />
        </el-form-item>
        <el-form-item label="驾校地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入驾校地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="校区管理人" prop="manager">
          <el-input v-model="form.manager" placeholder="请输入校区管理人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCampus, getCampus, addCampus, updateCampus, deleteCampus } from '@/api/campus'
export default {
  name: 'Campus',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 驾校表表格数据
      campusList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        name: null,
        startTime: null,
        endTime: null
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '用户名不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查询驾校表列表
    getList() {
      this.loading = true
      if (this.dateRange) {
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }
      listCampus(this.queryParams).then((response) => {
        this.campusList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        status: '0',
        createTime: null,
        updateTime: null
      }
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      this.dateRange = []
      this.queryParams.name = null
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 新增按钮操作
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加驾校信息'
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.reset()
      getCampus(row.id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改驾校信息'
      })
    },
    // 提交按钮
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCampus(this.form).then((response) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.open = false
              this.getList()
            })
          } else {
            addCampus(this.form).then((response) => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    // 删除按钮操作
    handleDelete(row) {
      const id = row.id
      this.$confirm('是否确认删除用户信息编号为"' + id + '"的数据项吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return deleteCampus(id)
        })
        .then(() => {
          this.getList()
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss" scoped>
.funOperate {
  display: flex;
  justify-content: space-between;
}
</style>
